package model

import (
	"toyota-client1/utils/tools"
)

type QueryData struct {
	PageIndex int    `json:"page"`
	PageSize  int    `json:"size"`
	Search    string `json:"search"`
	StartTime string `json:"start_time"`
	EndTime   string `json:"end_time"`
}

func (q *QueryData) Initialize() {
	if q.PageIndex == 0 {
		q.PageIndex = 1
	}
	if q.PageSize == 0 {
		q.PageSize = 10
	}
}

const TimeFormat = "2006-01-02 15:04:05"

func (q *QueryData) TimeHandle(t int) {
	if t == 0 { // 参数为datetime, 秒级初始化
		if q.StartTime != "" {
			q.StartTime = tools.StrToTime(q.StartTime).Format(TimeFormat)
		}
		if q.EndTime != "" {
			q.EndTime = tools.StrToTime(q.EndTime).Format(TimeFormat)
		}
	}
	if t == 1 { // 参数为date, 秒级初始化
		if q.StartTime != "" {
			q.StartTime = tools.StrToTime(q.StartTime).Format("2006-01-02") + " 00:00:00"
		}
		if q.EndTime != "" {
			q.EndTime = tools.StrToTime(q.EndTime).Format("2006-01-02") + " 23:59:59"
		}
	}
	if t == 2 { // 参数为date, 天初始化
		if q.StartTime != "" {
			q.StartTime = tools.StrToTime(q.StartTime).Format("2006-01-02")
		}
		if q.EndTime != "" {
			q.EndTime = tools.StrToTime(q.EndTime).Format("2006-01-02")
		}
	}
}

func (q *QueryData) StartPage() int {
	return (q.PageIndex - 1) * q.PageSize
}
